package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 报表操作
 */
@RestController
@RequestMapping("/report")
public class ReportController {
    //会员数量折线图数据
    @Reference
    private MemberService memberService;
    @Reference
    private SetmealService setmealService;

    //会员数量折线图
    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        //System.out.println("1sdxjvbxkvb jjk.sbvSK>nkbb--------------------------");
        //将月份及数量存入map集合
        Map<String , Object> map = new HashMap<>();
        //将月份存入list集合
        List<String> months = new ArrayList<>();
        //获取当前月份
        Calendar calendar = Calendar.getInstance();
        //计算过去一年的月份
        calendar.add(Calendar.MONTH,-12);
        //遍历取出没一个月份
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);
            Date date = calendar.getTime();
           months.add(new SimpleDateFormat("yyyy-MM").format(date));

        }
        //将月份出入map集合中
        map.put("months",months);
        //根据月份对应的数据查询存入list 集合
        List<Integer> memberCount = memberService.findMemberCountByMonths(months);
        map.put("memberCount",memberCount);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }
    //套餐预约占比饼形图
    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
        //使用模拟数据测试使用什么样的java对象转为饼形图所需的json数据格式
        Map<String,Object> data = new HashMap<>();

//        List<String> setmealNames = new ArrayList<>();
//        setmealNames.add("体检套餐");
//        setmealNames.add("孕前检查套餐");
//        data.put("setmealNames",setmealNames);
       // System.out.println("2222222222222222222222222");
        try{
            List<Map<String,Object>> setmealCount = setmealService.findSetmealCount();
            data.put("setmealCount",setmealCount);

            List<String> setmealNames = new ArrayList<>();
            for (Map<String, Object> map : setmealCount) {
                String name = (String) map.get("name");//套餐名称
                setmealNames.add(name);
            }

            data.put("setmealNames",setmealNames);
            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,data);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    @Reference
    private ReportService reportService;

    //运营数据统计
    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        try{
            Map<String,Object> data = reportService.getBusinessReportData();
            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,data);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }
}
